<?php
namespace common\helps;
use Yii;
/**
 *文本检测相关类
 * @author hey_heyang(635870453@qq.com)
 */


class detection{
    protected $client_id='wx08f5b9990c34746d';//APP key
    protected $client_secret='769f0341f1b1faa807a60758ff336295';//APP secret_key
    protected $access_token_url= 'https://api.weixin.qq.com/cgi-bin/token'; //获取小程序access_token请求地址
    protected $check_url= 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token=';//验证敏感词汇请求地址
    protected $img_check_url= 'https://api.weixin.qq.com/wxa/img_sec_check?access_token=';//验证图片请求地址


    public  function  getAccessToken(){  //获取小程序access_token
        $res = $this->request_post();
        $res = json_decode($res,true);
        if(!empty($res['access_token'])){
            return $res['access_token'];
        }else{
            return false;
        }

//            $redis = Yii::$app->cache;
//            $access_token = $redis->get('accesstoken');
//            if(!empty($access_token)){
//                return $access_token;
//            }else{
//                $res = $this->request_post();
//                $res = json_decode($res,true);
//                if(!empty($res['access_token'])){
//                    $redis->set('accesstoken',$res['access_token'],'30');
//                    return $res['access_token'];
//                }else{
//                    return false;
//                }
//            }
        }


    function request_post() { //请求小程序access_token
        $url = $this->access_token_url.'?grant_type=client_credential&appid='.$this->client_id.'&secret='.$this->client_secret;
        $ch  = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);

        return $data;
    }


    function  checkWord($content){  // 小程序监测敏感词汇
        $ACCESS_TOKEN=$this->getAccessToken();
        $data['content'] = $content;
        $res = $this->request_check($this->check_url.$ACCESS_TOKEN, $data);
        if($res['errcode']!=0 && $res['errcode']!=87014 ){
            return false;
        }
        return $res;
    }

    function  checkImg($file){  // 小程序图片检测
        $ACCESS_TOKEN=$this->getAccessToken();
        $data['media'] = $file;
        $res = $this->request_check($this->img_check_url.$ACCESS_TOKEN, $data,'img');
        if($res['errcode']!=0 && $res['errcode']!=87014 ){
            return false;
        }
        return $res;
    }


    function request_check($url = '', $data,$type='') {  //接口验证敏感词汇
        if (empty($url) || empty($data)) {
            return false;
        }
        $postUrl = $url;
        if($type=='img'){
            $curlPost = $data;
        }else{
            $curlPost = json_encode($data,JSON_UNESCAPED_UNICODE);//$content(需要检测的文本内容，最大520KB)
        }
        $curl = curl_init();//初始化curl
        curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($curl);//运行curl
        curl_close($curl);

        return json_decode($data,true);
    }



}
